package com.example.home.fragment.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.active_function.CommentBean;
import com.example.home.fragment.adapter.CommentAdapter;
import com.example.home.fragment.adapter.RecommendListAdapter;
import com.example.home.fragment.bean.InDetailBean;
import com.example.home.fragment.bean.NewsCommentBean;
import com.example.home.fragment.bean.RecommendListBean;
import com.example.home.government.acivity.SendAppeal;
import com.example.home.house.activity.HouseDetailsActivity;
import com.example.home.house.bean.HouseListBean;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Comment;

import static com.example.home.tool.StaticActi.ip;
import static com.example.home.tool.StaticActi.mimeType;

public class NewsDetailsActivity extends AppCompatActivity {
    private ImageView back;
    private ImageView imgUrl;
    private WebView newText;
    private RecyclerView recComment;
    private TextView tvComment;
    private EditText newsContent;
    private Button newsbutView;
    private TextView newsDetailContent;
    private RecyclerView recList;
    private String listId;
    private InDetailBean inDetailBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_details);
        Intent intent = getIntent();
        listId = intent.getStringExtra("NewsFram_id");
        initView();
        getInDetail();
        getComment();
        getRecommendList();
    }

    private void getRecommendList() {
        OKHttpTool.doNetRequest("prod-api/api/park/press/press/list",
                OKHttpTool.GET,
                RecommendListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RecommendListBean recommendListBean = (RecommendListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(recommendListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindList(RecommendListBean recommendListBean) {
        RecyclerView recyclerView = findViewById(R.id.rec_list);
        RecommendListAdapter recommendListAdapter = new RecommendListAdapter(NewsDetailsActivity.this,recommendListBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsDetailsActivity.this);
        recyclerView.setAdapter(recommendListAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }


    private void getComment() {
        OKHttpTool.doNetRequest("prod-api/api/park/press/comments/list",
                OKHttpTool.GET,
                NewsCommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        NewsCommentBean newsCommentBean = (NewsCommentBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindCommentList(newsCommentBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindCommentList(NewsCommentBean newsCommentBean) {
        RecyclerView recyclerView = findViewById(R.id.rec_comment);
        CommentAdapter commentAdapter = new CommentAdapter(NewsDetailsActivity.this,newsCommentBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsDetailsActivity.this);
        recyclerView.setAdapter(commentAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void getInDetail() {
        OKHttpTool.doNetRequest("prod-api/press/press/"+listId,
            OKHttpTool.GET,
                InDetailBean.class,
            new OKHttpTool.NetCallback() {
                @Override
                public void onSuccess(Object obj) {
                    inDetailBean = (InDetailBean) obj;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(NewsDetailsActivity.this).load(ip+ inDetailBean.getData().getCover()).into(imgUrl);
                            newText.loadDataWithBaseURL(null,inDetailBean.getData().getContent(),mimeType,"utf-8",null);
                            tvComment.setText("当前有"+ inDetailBean.getData().getCommentNum()+"条评论");
                        }
                    });
                }

                @Override
                public void onFailure(String msg) {

                }
            });

    }

    private void initView() {
        back = findViewById(R.id.back);
        imgUrl = findViewById(R.id.imgUrl);
        newText = findViewById(R.id.new_text);
        recComment = findViewById(R.id.rec_comment);
        tvComment = findViewById(R.id.tv_comment);
        newsContent = findViewById(R.id.news_content);
        newsbutView = findViewById(R.id.newsbut_view);
        newsDetailContent = findViewById(R.id.newsDetailContent);
        recList = findViewById(R.id.rec_list);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsDetailsActivity.this.finish();
            }
        });

        newsbutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = newsContent.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("comment",text);
                    jsonObject.put("newsId",listId);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (!TextUtils.isEmpty(text)){
                    OKHttpTool.doNetRequest("prod-api/api/park/press/pressComment",
                            OKHttpTool.POST,
                            GeneralClass.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    GeneralClass generalClass = (GeneralClass) obj;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (generalClass.getCode() == 200) {
                                                Toast.makeText(NewsDetailsActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
                                            } else if (generalClass.getCode() == 500) {
                                                Toast.makeText(NewsDetailsActivity.this, "评论失败", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {

                                }
                            },jsonObject.toString());
                }
            }
        });
    }
}